﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using iTFF.WPF.CP;
using System.Windows.Input;
using System.Windows.Controls;
using iTFF.WPF.Model.Toaster;
using System.Windows;
using iTFF.WPF.Resources.Languages;
using System.Globalization;
using iTFF.WPF.Model.Artwork;

namespace iTFF.WPF.ViewModel
{
    class PreferencesViewModel : WorkspaceViewModel
    {

        #region Fields

        ToasterSkinViewModel _selectedToasterSkin;
        UserControl _selectedToasterSkinUC;

        RelayCommand _showToasterCommand;

        #endregion

        #region Contructor

        public PreferencesViewModel()
        {
            // init toasters skins list
            ToasterSkins = new ObservableCollection<ToasterSkinViewModel>(
                ControlPanel.Instance.ToasterControlPanel.Skins.Select(s => new ToasterSkinViewModel(s)));

            base.DisplayName = Strings.PreferencesViewModelDisplayName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The toasters collection
        /// </summary>
        public ObservableCollection<ToasterSkinViewModel> ToasterSkins { get; set; }

        /// <summary>
        /// Gets or sets the selected toaster skin
        /// </summary>
        public ToasterSkinViewModel SelectedToasterSkin
        {
            get
            {
                if (_selectedToasterSkin == null)
                {
                    _selectedToasterSkin = ToasterSkins.FirstOrDefault(s => s.Name == Settings.Default.ToasterSelectedSkin);

                    // refresh the uc if the user has a correct selected skin
                    if (_selectedToasterSkin != null && _selectedToasterSkinUC == null)
                        SelectedToasterSkinUC = CreateUCForSelectedToasterSkin(_selectedToasterSkin.FullName);
                }

                return _selectedToasterSkin;
            }
            set
            {
                _selectedToasterSkin = value;

                Settings.Default.ToasterSelectedSkin = value.Name;
                if (value != null)
                    SelectedToasterSkinUC = CreateUCForSelectedToasterSkin(value.FullName);

                OnPropertyChanged("SelectedToasterSkin");

            }
        }

        /// <summary>
        /// Gets or sets the current toaster skin, for preview
        /// </summary>
        public UserControl SelectedToasterSkinUC
        {
            get { return _selectedToasterSkinUC; }
            set
            {
                if (_selectedToasterSkinUC != value)
                {
                    _selectedToasterSkinUC = value;
                    OnPropertyChanged("SelectedToasterSkinUC");
                }
            }
        }

        #endregion

        #region Settings properties

        public bool EnableAutoToaster
        {
            get { return Settings.Default.ToasterEnable; }
            set
            {
                if (Settings.Default.ToasterEnable != value)
                {
                    Settings.Default.ToasterEnable = value;
                    OnPropertyChanged("EnableAutoToaster");
                }
            }
        }

        public int ShowToasterTimer
        {
            get { return Settings.Default.ToasterShowTimer; }
            set
            {
                if (Settings.Default.ToasterShowTimer != value)
                {
                    Settings.Default.ToasterShowTimer = value;
                    OnPropertyChanged("ShowToasterTimer");
                }
            }
        }

        public bool HideToasterOnClick
        {
            get { return Settings.Default.ToasterHideOnClick; }
            set
            {
                if (Settings.Default.ToasterHideOnClick != value)
                {
                    Settings.Default.ToasterHideOnClick = value;
                    OnPropertyChanged("HideToasterOnClick");
                }
            }
        }

        public bool ReappearToasterOnMouseOver
        {
            get { return Settings.Default.ToasterReappearOnMouseOver; }
            set
            {
                if (Settings.Default.ToasterReappearOnMouseOver != value)
                {
                    Settings.Default.ToasterReappearOnMouseOver = value;
                    OnPropertyChanged("ReappearToasterOnMouseOver");
                }
            }
        }

        public CultureInfo SelectedCulture
        {
            get { return ControlPanel.Instance.UserCultureInfo; }
            set
            {
                if (ControlPanel.Instance.UserCultureInfo != value)
                    ControlPanel.Instance.UserCultureInfo = value;
            }
        }

        public CultureInfo[] AvailableCultures
        {
            get { return ControlPanel.Instance.AvailableCultures; }
        }

        public AmazonLocale ArtworkSelectedAmazonLocale
        {
            get { return ControlPanel.Instance.ArtworksControlPanel.SelectedAmazonLocale; }
            set { ControlPanel.Instance.ArtworksControlPanel.SelectedAmazonLocale = value; }
        }

        public AmazonLocale[] AvailableAmazonLocales
        {
            get { return ControlPanel.Instance.ArtworksControlPanel.AmazonLocales; }
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Creates the uc for the selected toaster skin
        /// </summary>
        private UserControl CreateUCForSelectedToasterSkin(string fullname)
        {
            try
            {
                var uc = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(fullname) as UserControl;

                if (uc != null)
                    uc.DataContext = CreateToasterTrackForExample();
                return uc;
            }
            catch (Exception)
            {
                // don't do nothing
                return null;
            }
        }

        private ToasterTrackViewModel CreateToasterTrackForExample()
        {

            return new ToasterTrackViewModel(new ToasterTrack()
            {
                Artist = "Incubus",
                Title = "A Certain Shade of Green",
                Album = "S.C.I.E.N.C.E.",
                ImageStream = Application.GetResourceStream(new Uri("../resources/images/incubus cover.png", UriKind.Relative)).Stream,
                Stars = 5
            })
            {
                ToasterSkin = SelectedToasterSkin
            };

        }

        #endregion

        #region Private helpers

        private ToasterSkin CurrentSkin
        {
            get { return ControlPanel.Instance.ToasterControlPanel.Skins.FirstOrDefault(s => s.Name == Settings.Default.ToasterSelectedSkin); }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Shows the toaster
        /// </summary>
        public ICommand ShowToasterCommand
        {
            get
            {
                if (_showToasterCommand == null)
                    _showToasterCommand = new RelayCommand(param => OnShowToasterCommand(param));
                return _showToasterCommand;
            }
        }

        /// <summary>
        /// Shows the toaster
        /// </summary>
        private void OnShowToasterCommand(object param)
        {
            ControlPanel.Instance.ToasterControlPanel.ShowToast();
        }

        #endregion
    }
}
